package constant.milk.cashbook;

import constant.milk.cashbook.auto.AutoManager;
import constant.milk.cashbook.database.DbAdapter;
import constant.milk.cashbook.util.MilkAd;
import constant.milk.cashbook.util.MilkTypeFace;
import constant.milk.cashbook.util.MilkUtil;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public abstract class SuperActivity extends Activity
{
	public static final float TEXT_TITLE_SIZE = 20; 
	public static final float TEXT_SIZE = 18; 
//	public static final int TEXT_COLOR = 0xff35484b; 
	public static final int TEXT_COLOR = 0xff3d3d3d; 
	
	public Context superContext = this;
	
	public DbAdapter superDb;
	
	private View titleLayout;
	private TextView titleView;
	private Button leftButton;
	private Button rightButton;
	private ViewGroup bodyLayout;
	
	private ViewGroup superAdView;
	
	private boolean isAdFlag = true;
	
	public String shape;
	
	private AutoManager autoManager;
	
	private MilkAd ad;
	
	private MilkTypeFace milkTypeFace;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		super.setContentView(R.layout.super_activity);
		
		titleLayout = findViewById(R.id.titleLayout);
		titleView = (TextView) findViewById(R.id.superTitleView);
		bodyLayout = (ViewGroup) findViewById(R.id.bodyLayout);
		leftButton = (Button) findViewById(R.id.superLeftButton);
		rightButton = (Button) findViewById(R.id.superRightButton);
		
		superAdView = (ViewGroup) findViewById(R.id.superAdview);
		
		leftButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				onBackPressed();
			}
		});
		
		rightButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				tipButton();
			}
		});
		
		superDb = DbAdapter.getInstance();
		
		autoManager = AutoManager.getInstance();
		
		if (milkTypeFace == null)
		{
			milkTypeFace = MilkTypeFace.getInstance(superContext);
		}
		
		if (isAdFlag)
		{
			ad = new MilkAd(superContext, (LinearLayout) superAdView);
		}
	}
	
	@Override
	protected void onResume()
	{
		super.onResume();
		
		String massage = autoManager.start(superContext, superDb);
		
		if (!massage.equals(""))
		{
			MilkUtil.showDialog(superContext, "알림", massage);
		}
		
		if (ad != null)
		{
			ad.resumeAd();
		}
	}
	
	@Override
	protected void onDestroy()
	{
		super.onDestroy();
		
		if (ad != null)
		{
			ad.destoryAd();
		}
	}
	
	@Override
	public void setContentView(int layoutResID)
	{
		LayoutInflater inflater = getLayoutInflater();
		View view = inflater.inflate(layoutResID, null);
		this.setContentView(view);
	}
	
	@Override
	public void setContentView(View view) {
		this.bodyLayout.removeAllViews();
		
		if(view == null)
		{
			return;
		}
		
		ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
		
		if(layoutParams == null)
		{
			layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		}
		else
		{
			layoutParams.width = LayoutParams.MATCH_PARENT;
			layoutParams.height = LayoutParams.MATCH_PARENT;
		}
		
		view.setLayoutParams(layoutParams);
		
		this.bodyLayout.addView(view);
		
		ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
		setGlobalFont(root);
	}
	
	@Override
	public void setTitle(CharSequence title)
	{
		titleView.setText(title);
	}

	@Override
	public void setTitle(int titleId)
	{
		titleView.setBackgroundResource(titleId);
	}
	
	/**
	 * 오른쪽 팁 버튼을 안보이게 한다.
	 */
	public void setGoneTipButton()
	{
		rightButton.setVisibility(View.INVISIBLE);
	}
	
	/**
	 * 타이틀을 안보이게 한다.
	 */
	public void setGoneTitle()
	{
		titleLayout.setVisibility(View.GONE);
	}
	
	/**
	 * 도움말 버튼의 문구를 변경한다.
	 * @param text
	 */
	public void setTextTip(String text)
	{
		rightButton.setText(text);
	}
	
	@Override
	protected void onNewIntent(Intent intent)
	{
		super.onNewIntent(intent);
		
		ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
		setGlobalFont(root);
	}
	
	private void setGlobalFont(ViewGroup root) 
	{
	    for (int i = 0; i < root.getChildCount(); i++) 
	    {
	        View child = root.getChildAt(i);
	        
	        if (child instanceof TextView)
	        {
	        	((TextView)child).setTypeface(milkTypeFace.getTypeface(superContext));
	        	
	        	((TextView)child).setTextSize(TEXT_SIZE);
	        	((TextView)child).setTextColor(TEXT_COLOR);
	        }
	        else if (child instanceof EditText)
			{
	        	((EditText)child).setTypeface(milkTypeFace.getTypeface(superContext));
	        	
	        	((EditText)child).setTextSize(TEXT_SIZE);
	        	((EditText)child).setTextColor(TEXT_COLOR);
			}
	        else if (child instanceof Button)
			{
	        	((Button)child).setTypeface(milkTypeFace.getTypeface(superContext));
	        	
	        	((Button)child).setTextSize(TEXT_SIZE);
	        	((Button)child).setTextColor(TEXT_COLOR);
			}
	        else if (child instanceof ViewGroup)
	        {
	        	setGlobalFont((ViewGroup)child);
	        }
	    }
	    
	    setTemaSetting();
	}

	private void setTemaSetting()
	{
		titleView.setTextSize(TEXT_TITLE_SIZE);
		titleLayout.setBackgroundResource(R.drawable.bg_top);
	    bodyLayout.setBackgroundResource(R.drawable.bg_mint);
	}
	
	/**
	 * xml의 아이디 값을 매칭 시킨다.
	 */
	protected abstract void setFindViewId();
	
	/**
	 * 아이디에 클릭리스너를 달아준다.
	 */
	protected abstract void setOnClick();
	
	/**
	 * 데이터를 셋팅한다.
	 */
	protected abstract void setData();
	
	/**
	 * 도움말 버튼입니다.
	 */
	protected abstract void tipButton();
}
